package com.ZengQi.event.view;



import com.ZengQi.event.controller.RegisController;
import com.ZengQi.event.entity.User;
import com.ZengQi.event.util.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class RegisView extends JFrame implements ActionListener {
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userAccountLabel = new JLabel("手机号");
    JTextField userAccountTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码");
    JPasswordField pwdField = new JPasswordField();
    JLabel userAgeLabel=new JLabel("生日");
    JTextField userAgeTxt = new JTextField();
    JLabel userNameLabel=new JLabel("昵称");
    JTextField userNameTxt = new JTextField();
    JLabel userSexLabel=new JLabel("性别");
    JTextField userSexTxt=new JTextField();


    JButton regisBtn = new JButton("注册");
    JButton resetBtn = new JButton("重置");


    RegisController regisController;

    public RegisView(){
        super("做题帮注册");
        regisController=new RegisController(this);

        Container contentPane = getContentPane();
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        regisBtn.setFont(centerFont);
        resetBtn.setFont(centerFont);
        userNameLabel.setFont(centerFont);
        userNameTxt.setPreferredSize(new Dimension(200, 30));
        userAccountLabel.setFont(centerFont);
        userAccountTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));
        userAgeLabel.setFont(centerFont);
        userAgeTxt.setPreferredSize(new Dimension(200, 30));
        userSexLabel.setFont(centerFont);
        userSexTxt.setPreferredSize(new Dimension(200,30));
        centerPanel.add(userSexLabel);
        centerPanel.add(userSexTxt);
        centerPanel.add(userNameLabel);
        centerPanel.add(userNameTxt);
        centerPanel.add(userAccountLabel);
        centerPanel.add(userAccountTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        centerPanel.add(userAgeLabel);
        centerPanel.add(userAgeTxt);
        centerPanel.add(regisBtn);
        centerPanel.add(resetBtn);

        layoutCenter();

       // 增加按键事件监听
       regisBtn.addActionListener(this);

        resetBtn.addActionListener(this);

        contentPane.add(centerPanel,BorderLayout.CENTER);

        //默认按钮
        getRootPane().setDefaultButton(regisBtn);

        URL resources = RegisView.class.getClassLoader().getResource("bang.png");

        Image image = new ImageIcon(resources).getImage();
        setIconImage(image);


        setSize(600, 400);
        setVisible(true);
        //居中
        setLocationRelativeTo(null);
        //关闭时退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //大小不可改变
        setResizable(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();



        User user=new User();
        user.setLoginPnum(getUserAccountTxt().getText());
        user.setLoginPwd(new String(getPwdField().getPassword()));
        user.setUserName(getUserNameTxt().getText());
        user.setUserBirthday(getUserAgeTxt().getText());
        user.setUserSex(getUserSexTxt().getText());


        if ("注册".equals(text)) {
            /**
             * 对输入的合法性进行判断，只需在注册时判断即可，登录时若输入不合法可直接弹出弹窗警告
             * 判断方法：遍历手机号字符串若出现数字以外的字符则弹出警告并清空
             *
             */
            boolean loginPnumIsNumber = StringUtils.isNumber(getUserAccountTxt().getText());
            if(!loginPnumIsNumber){
                JOptionPane.showMessageDialog(this,"请输入正确格式的手机号！");
                getUserAccountTxt().setText("");
                return;
            }
           regisController.regis(user);
        } else if("重置".equals(text)){
            getUserNameTxt().setText("");
            getPwdField().setText("");
           getUserAccountTxt().setText("");
            getUserAgeTxt().setText("");
            getUserSexTxt().setText("");

        }
    }

    public void layoutCenter(){
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userNameTxt)), Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 10, SpringLayout.NORTH, centerPanel);

        springLayout.putConstraint(SpringLayout.WEST, userNameTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameTxt, 0, SpringLayout.NORTH, userNameLabel);

        springLayout.putConstraint(SpringLayout.EAST, userAccountLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userAccountLabel, 20, SpringLayout.SOUTH, userNameLabel);

        springLayout.putConstraint(SpringLayout.WEST, userAccountTxt, 20, SpringLayout.EAST, userAccountLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userAccountTxt, 0, SpringLayout.NORTH, userAccountLabel);

        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userAccountLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userAccountTxt);

        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);

       springLayout.putConstraint(SpringLayout.EAST, userAgeLabel, 0, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userAgeLabel, 20, SpringLayout.SOUTH, pwdLabel);

       springLayout.putConstraint(SpringLayout.WEST, userAgeTxt, 20, SpringLayout.EAST, userAgeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userAgeTxt, 0, SpringLayout.NORTH, userAgeLabel);

        springLayout.putConstraint(SpringLayout.EAST, userSexLabel, 0, SpringLayout.EAST, userAgeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userSexLabel, 20, SpringLayout.SOUTH, userAgeLabel);

        springLayout.putConstraint(SpringLayout.WEST, userSexTxt, 20, SpringLayout.EAST, userSexLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userSexTxt, 0, SpringLayout.NORTH, userSexLabel);






        springLayout.putConstraint(SpringLayout.WEST, regisBtn, 50, SpringLayout.WEST, userAgeLabel);
        springLayout.putConstraint(SpringLayout.NORTH, regisBtn, 60, SpringLayout.SOUTH, userAgeTxt);

        springLayout.putConstraint(SpringLayout.WEST, resetBtn, 60, SpringLayout.EAST, regisBtn);
        springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0, SpringLayout.NORTH, regisBtn);



    }



    public JTextField getUserAccountTxt() {
        return userAccountTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public JTextField getUserNameTxt() {
        return userNameTxt;
    }

    public JTextField getUserAgeTxt() {
        return userAgeTxt;
    }

    public JTextField getUserSexTxt() {
        return userSexTxt;
    }
}
